<?php
require '../includes/master_include.php';
switch($_GET['action']){
	case 'cache':
		cache_artists();
		break;
	case 'menu':
		get_menu_item();
		break;	
	case 'cloud':
		get_tag_cloud();
		break;
	case 'cache_tags':
		cache_tags();
		break;
	default;
		break;
}

function cache_artists(){
	$username = $_COOKIE['user'];
	$user = user::get($username);
	if(!$user){
		$user = new user();
		$user->username = $username;
		$user->save();
	}
	$page = $_GET['page'];
	$batch_size = $_GET['batch_size'];
	
	$progress = library_cache_artists($user, $page, 50, $batch_size);
	print json_encode($progress);
}

function cache_tags(){
	$username = $_COOKIE['user'];
	$user = user::get($username);
	if(!$user){
		$user = new user();
		$user->username = $username;
		$user->save();
	}
	$continue = library_cache_tags($user, 50);
	print json_encode(array('continue' => $continue));
}

function get_tag_cloud(){
	$username = $_COOKIE['user'];
	$user = user::get($username);
	if($user){
		print json_encode(array('tags'=>$user->tags()));
	}else{
		print json_encode(array('tags'=>array()));
	}
}


function get_menu_item(){
	$user = $_COOKIE['user'];
	$initial = $_GET['initial'];
	$artists = array('initial' => $initial, 'artists' => library_artists($user, $initial));
	print json_encode($artists);
}
?>